package com.sj.排序算法;

import java.lang.management.GarbageCollectorMXBean;
import java.util.Arrays;

/**
 * @author sj
 * @date 2021/5/2
 **/
public class 希尔排序 {
    public static void main(String[] args) {
        int[] arr = {9,6,11,2,3,5,8,12,7,9,14,4,3,5};
        sort(arr);
        System.out.println(Arrays.toString(arr));

    }
    public static void sort(int[] arr){
        int h = 1;
        while (h <= arr.length / 3){
            h = h * 3 + 1;
        }
        for (int gap = h ; gap > 0 ; gap = (gap-1)/3){
            for (int i = gap; i < arr.length ; i++){
                for (int j = i;j > gap - 1; j -=gap){
                    if (arr[j] < arr[j - gap]){
                        swap(arr,j,j-gap);
                    }
                }
            }
        }
    }
    //两数交换
    public static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

}
